<!-- CHART STUFF -->
<script type="text/javascript" src="../local/webfxlayout.js"></script>
<script type="text/javascript" src="../includes/excanvas.js"></script>
<script type="text/javascript" src="../includes/wz_jsgraphics.js"></script>
<script type="text/javascript" src="../includes/chart.js"></script>
<script type="text/javascript" src="../includes/canvaschartpainter.js"></script>
<script type="text/javascript" src="../includes/jgchartpainter.js.js"></script>
<script type="text/javascript" src="../includes/demo.js"></script>
<link rel="stylesheet" type="text/css" href="../includes/canvaschart.css">

<%@page import="bridge.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.text.DecimalFormat"%>

<% 
	double min_value = 0;
	double max_value = 100;
	String unit = "";
	long window = 1000;	// milliseconds
	long refresh = 1000;
	long hours = 0;
	long minutes = 0;
	long seconds = 0;
	String last_time = "";
	String middle_time = "";
	ArrayList<Pair<Long,Float>> pairs = null;
	float[] results = null;
	float zoom = 1F;
	try
	{
		byte sensor_id = Byte.parseByte(request.getParameter("sensorid"));
		window = Long.parseLong(request.getParameter("window"));
		refresh = Long.parseLong(request.getParameter("refresh"));
		
		DatabaseAccessor dba = DatabaseAccessor.getInstance();
		pairs = dba.getTailData(sensor_id, window);
		//results = dba.formatData(pairs, window, 800);
//		out.println("<!--" + dba.formatData(pairs, window, 300) + "-->");
		
		
		Sensor sensor = dba.getSensor(sensor_id);
		
		if( sensor_id== 21)
		{
			zoom = 2F;
		}
		
		if(sensor == null)
			return;
			
		SensorType type = sensor.type;
		unit = type.unit;
		min_value = type.min / zoom;
		max_value = type.max / zoom;
		long temp = window;
	
		seconds = window/1000;
		hours = seconds / 3600;
		long tempminutes = seconds % 3600;
		minutes = tempminutes / 60;
		seconds = tempminutes % 60;
	
		if(hours > 0)
		{	
			if(hours > 1)
				last_time += hours + " hours ";
			else
				last_time += hours + " hour ";
		}
		if(minutes > 0)
		{
			if(minutes > 1)
				last_time += minutes + " minutes ";
			else
				last_time += minutes + " minute";
		}
		if(seconds > 0)
		{
			if(seconds > 1)
				last_time += seconds + " seconds";
			else
				last_time += seconds + " second";
		}
		
		seconds = window/2 / 1000;
		hours = seconds / 3600;
		tempminutes = seconds % 3600;
		minutes = tempminutes / 60;
		seconds = tempminutes % 60;
		
		if(hours > 0)
		{	
			if(hours > 1)
				middle_time += hours + " hours ";
			else
				middle_time += hours + " hour ";
		}
		if(minutes > 0)
		{
			if(minutes > 1)
				middle_time += minutes + " minutes ";
			else
				middle_time += minutes + " minute";
		}
		if(seconds > 0)
		{
			if(seconds > 1)
				middle_time += seconds + " seconds";
			else
				middle_time += seconds + " second";
		}
		
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
%>

<script type="text/javascript">
function draw_chart() {
	
	var c = new Chart(document.getElementById('chart'));
	c.setDefaultType(CHART_LINE);
	// grid density (x, y) must be less than or equal to the number of labels
	c.setGridDensity(11, 3);
	c.setHorizontalLabels(['<%=last_time%>', '', '', '', '', '<%=middle_time%>', '', '', '', '','']);
	c.setShowLegend(false);
	c.setVerticalRange(<%=min_value%>, <%=max_value%>);
	c.setUnit('<%=unit%>');
	a = 
	[<% 
		DecimalFormat df = new DecimalFormat("0.####");
		for(int k = 0; k < pairs.size(); k++)
		{	
			if(k != 0) {%>,<%}%><%=df.format(pairs.get(k).snd * zoom * (max_value - min_value) + min_value)%>
	<%
		}
	%>
	];
	c.add('', '#C23939', a);
	
	c.draw();
}
</script>

<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="<%=(refresh/1000.0)%>">
</head>
<body onload="draw_chart();">
<div id="chart" class="chart" style="width:100%; height: 100%;">
<%="Done"%>
</body>
</html>